package com.lfz.rabbit.async;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.AsyncRabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

@Component
public class AsyncRPCClient {

    @Autowired(required=false)
    AsyncRabbitTemplate asyncRabbitTemplate;

    @Autowired
    AmqpTemplate amqpTemplate;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Async
    public Future<String> sendAsync(String message, String asyncRpcQueueName) {
        String result = (String) amqpTemplate.convertSendAndReceive(asyncRpcQueueName, message);
        return new AsyncResult<>(result);
    }

    @Async
    public Future<String> sendWithFixedReplay(String message, String asyncRpcFixedReplyQueueName) {
        return asyncRabbitTemplate.convertSendAndReceive(asyncRpcFixedReplyQueueName, message);
    }

}